package com.example.mobile2032021606129ly0415;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

//RectclerView适配器
//2.适配器继承 RecyclerView.adapter，指定范类当中的类型，指定范类为内部viewholder


public class PeopleAdapter extends RecyclerView.Adapter<PeopleAdapter.ViewHolder>{

    String[] names;
    public  PeopleAdapter(String[]names){
        this.names=names;
    }



    //1.定义viewholder静态内部类，在类中获取列表文件的控件
    //定义一个类来描述列表项文件
    //static无需创造对象（无static必须要有对象，就被封进peopleadapter）
    static class  ViewHolder extends RecyclerView.ViewHolder{
        TextView tvItemName;


     public ViewHolder(@NonNull View itemView) {
         super(itemView);
         //对控件进行初始化
         tvItemName=itemView.findViewById(R.id.rv_main_people);
         //把item——tv（列表项文件）转化为java代码，主程序方可控制或引用它
     }
 }
//3.添加父类的抽象方法
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //
       View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_main_people,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //给控件赋值
        holder.tvItemName.setText(names[position]);

    }

    @Override
    public int getItemCount() {
        return names.length;
    }
}
